package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import u.aly.bs;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.NumberFormatUtil;
import com.muqi.app.qlearn.adapter.PersonAdapter;
import com.muqi.app.qlearn.adapter.RewardsAndMovableAdapter;
import com.muqi.app.qlearn.adapter.RewardsAndMovableAdapter.OnNextArrow;
import com.muqi.app.qlearn.modles.GrowRewardAndMoveInfo;
import com.muqi.app.qlearn.modles.PersonBean;
import com.muqi.app.qlearn.modles.RecordDetailBean;
import com.muqi.app.qlearn.publish.RecordDetailActivity;
import com.muqi.app.qlearn.student.BaseChildFragment;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.ui.MoneyPayActivity;

import com.muqi.app.user.db.ChildBean;
import com.muqi.app.widget.pulltorefresh.XListView;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

public class InteractiveFragment extends BaseFragment implements
		OnClickListener, XListView.IXListViewListener, OnItemClickListener,OnItemLongClickListener
		{
	protected int uiType = 0; /* 0 粉丝列表 1关注列表 */
	private XListView mListview;

	private TextView mTv_title;
	private int page = 1;

	private List<PersonBean> infos = new ArrayList<PersonBean>();
	private PersonAdapter adapter;
    private Intent intent;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		uiType = args.getInt("type");
		intent=new Intent(MoneyPayActivity.PAY_WITH_SUCCESS);
	}

	@SuppressLint("InflateParams")
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		return LayoutInflater.from(mActivity).inflate(
				R.layout.fragment_friend, null);

	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mTv_title = (TextView) getActivity().findViewById(R.id.tv_title);

		if (uiType == 0) {

			mTv_title.setText("我的粉丝");
		} else if (uiType == 1) {

			mTv_title.setText("我的关注");
		}

		mListview = (XListView) view.findViewById(R.id.reword_lv);
		mListview.setPullLoadEnable(false);
		mListview.setPullRefreshEnable(true);
		adapter = new PersonAdapter(mActivity, infos);
		mListview.setAdapter(adapter);
		mListview.setOnItemClickListener(this);
		mListview.setXListViewListener(this);
		mListview.setOnItemLongClickListener(this);

		loadData();

	}

	/**
	 * 加载数据
	 */
	private void loadData() {
		getListData();
	}

	private void getListData() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("page", page);
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		String requestUrl = "";
		if (uiType == 0) {
			sendMap.put("type", "fans");
			requestUrl = ParamsUtils.buildParams(
					NetWorkApi.MY_FUN_API, sendMap);
			
		} else {
			sendMap.put("type", "follow");
			requestUrl = ParamsUtils.buildParams(
					NetWorkApi.MY_CONCERN_API, sendMap);
			
		}
		MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,String result) {
				mListview.stopLoadMore();
				mListview.stopRefresh();
				mListview.setPullLoadEnable(false);
				if (resuleCode == RESULTCODE.SUCCESS) {
					ArrayList<PersonBean> tempList = ResponseUtils.getFollowList(mActivity, result);
		
					if (tempList != null) {
						if(page == 1){
							if(tempList.size()==0){
								showEmpty("还没有相关数据");
								return ;
							}else{
								hidEmpty();
							}
							infos = tempList;
							if(adapter!=null)adapter=null;
							adapter = new PersonAdapter(mActivity, infos);
							mListview.setAdapter(adapter);
						}else{
							for (PersonBean personBean : tempList) {
								infos.add(personBean);
							}
							if(adapter!=null) adapter.notifyDataSetChanged();
						}
						
						if (tempList.size() >= 10) {
							mListview.setPullLoadEnable(true);
							page++;
						} else {
							mListview.setPullLoadEnable(false);
						}
						
					}
				}

			}
		});

	}

	

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onRefresh() {
		page = 1;
		getListData();
	}

	@Override
	public void onLoadMore() {
		getListData();
	}

	//
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2,
			final long arg3) {
		if(uiType==1){
			new SweetAlertDialog(getActivity(), SweetAlertDialog.NORMAL_TYPE)
			.setTitleText("是否取消该关注？")
			.setCancelText("取消")
			.setConfirmText("确定")
			.showCancelButton(true)
			.setCancelClickListener(new OnSweetClickListener() {
				@Override
				public void onClick(SweetAlertDialog mDialog) {
					mDialog.dismissWithAnimation();
				}
			})
			.setConfirmClickListener(new OnSweetClickListener() {
				@Override
				public void onClick(SweetAlertDialog mDialog) {
					mDialog.dismissWithAnimation();
				
					cancelFollowPerson(infos.get(arg2-1).id);
				}

			
			}).show();
		}
		return true;
	}
     /**
      *  取消关注
      * @param id
      */
	protected void cancelFollowPerson(String id) {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("id", id);
		sendMap.put("action", "no");
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.MY_CONCERN_ACTION_API, sendMap);
		MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {
					Toast.makeText(mActivity, "取消关注", Toast.LENGTH_SHORT).show();
					getListData();
					getActivity().sendBroadcast(intent);
				}
			}
		});
	}

	


}